//
//  DemoAController.swift
//  SwiftBindDemo
//
//  Created by 万孟超 on 2023/12/27.
//

import UIKit
import CSBind

class DemoAController: UIViewController {
    
    class ModelA: NSObject {
        @objc dynamic var a: String = ""
    }
    
    class ModelB: NSObject {
        @objc dynamic var b: String = ""
    }
    
    let aModel = ModelA()
    let bModel = ModelB()
    
    var bindDispose: CSBindDispose?
    
    var index: Int = 0

    override func viewDidLoad() {
        super.viewDidLoad()
        aModel.a = "这个是a的原始值"
        bModel.b = "这个是b的原始值"
        // Do any additional setup after loading the view.
    }
    @IBAction func bind(_ sender: Any) {
        bindDispose = (aModel, \.a) ==> (bModel, \.b)
        bindDispose?.bindIgnoreTimes = 2
        bindDispose?.immediatelyAvaliable()
        Alert.show("开启单向绑定，ModelA.a的值会随着ModelB.b的值改变而改变", on: self)
    }
    @IBAction func dbind(_ sender: Any) {
        bindDispose = (aModel, \.a) <==> (bModel, \.b)
        Alert.show("开启双向绑定，ModelA.a的值和ModelB.b的值和相互改变", on: self)
    }
    
    @IBAction func changeA(_ sender: Any) {
        index += 1
        aModel.a = "这个是a的改变后的值\(index)"
        showAlert()
    }
    
    @IBAction func changeB(_ sender: Any) {
        index += 1
        bModel.b = "这个是b的改变后的值\(index)"
        showAlert()
    }
    @IBAction func debind(_ sender: Any) {
        bindDispose?.pause()
    }
    
    @IBAction func restart(_ sender: Any) {
        bindDispose?.run()
    }
    
    @IBAction func free(_ sender: Any) {
        bindDispose?.free()
    }
    
    func showAlert() {
        let message = "ModelA.a：\(String(describing: aModel.a))\nModelB.b：\(String(describing: bModel.b))"
        Alert.show(message, on: self)
    }
}
